<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 用户搜索 -->
      <el-form-item label="用户" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="时间" prop="date">
        <el-date-picker clearable v-model="queryParams.date" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="verifyState">
        <el-select v-model="queryParams.verifyState" @change="handleQuery" clearable>
          <el-option v-for="item in verifyList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="货币" prop="moneyId">
        <el-select v-model="queryParams.moneyId" @change="handleQuery" clearable>
          <el-option v-for="item in moneyList" :key="item.id" :label="item.moneyName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['admin:withdrawRecord:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        总金额:{{ statisticsData }}
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货币名称" align="center" prop="moneyName" />
      <el-table-column label="提现金额" align="center" prop="withdrawAmount" />
      <el-table-column label="手续费" align="center" prop="serviceCharge" />
      <el-table-column label="实际打款金额" align="center" prop="realAmount" />
      <el-table-column label="打款时间" align="center" prop="paymentAmount" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.paymentAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center">
        <template #default="scope">
          {{ scope.row.status == 1 ? '已打款' : '未打款' }}
        </template>
      </el-table-column>
      <el-table-column label="创建信息" align="center">
        <template #default="scope">
          <div class="">
            <div class="">
              用户:{{ scope.row.createUserTemp.nickName }}
            </div>
            <div class="">
              手机号:{{ scope.row.createUserTemp.phonenumber }}
            </div>
            <div class="">
              创建时间:{{ scope.row.createTime }}
            </div>
            <div class="">
              开户行姓名:{{ scope.row.bankCardInfo?.realName }}
            </div>
            <div class="">
              开户银行:{{ scope.row.bankCardInfo?.openBank }}
            </div>
            <div class="">
              卡号:{{ scope.row.bankCardInfo?.cardNo }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="审核信息" align="center">
        <template #default="scope">
          <div class="" v-if="scope.row.verifyUserTemp">
            <div class="">
              审核人:{{ scope.row.verifyUserTemp.userName }}
            </div>
            <div class="">
              审核状态:{{ scope.row.verifyState == 0 ? '未审核' : scope.row.verifyState == 1 ? '审核通过' : '不通过' }}
            </div>
            <div class="">
              审核时间:{{ parseTime(scope.row.verifyTime) }}
            </div>
            <div class="">
              审核备注:{{ scope.row.verifyRemark }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleAudit(scope.row)"
            v-hasPermi="['admin:withdrawRecord:edit']" v-if="scope.row.verifyState == 0">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="审核" v-model="open" width="500px" append-to-body>
      <el-form ref="forms" :model="form" label-width="80px">
        <el-form-item label="审核状态" prop="verifyState">
          <el-radio-group v-model="form.verifyState">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="verifyRemark">
          <el-input v-model="form.verifyRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WithdrawRecord">
import { listWithdrawRecord, verify, getWithdrawStatistics } from "@/api/admin/withdrawRecord";
import { listMoney } from "@/api/money/money";
import { ref } from "vue";

const { proxy } = getCurrentInstance();

const withdrawRecordList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const open = ref(false);
const statisticsData = ref('--');
const route = useRoute();

const verifyList = ref([
  {
    label: "未审核",
    value: 0
  },
  {
    label: "审核通过",
    value: 1
  },
  {
    label: "审核不通过",
    value: 2
  }
])

const verifyRemarkRule = (rule, value, callback) => {
  if (form.value.verifyState == 0) {
    if (!value) {
      return callback(new Error('请输入审核不通过原因!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    moneyId: null,
    withdrawAmount: null,
    withdrawWay: null,
    bankCard: null,
    cardNo: null,
    realName: null,
    mobile: null,
    serviceCharge: null,
    realAmount: null,
    paymentAmount: null,
    status: null,
    verifyState: null,
    verifyBy: null,
    verifyTime: null,
    withdrawType: route.query.withdrawType,
  },
  form: {},
  rules: {
    verifyState: [
      {
        require: true,
        message: "请选择审核状态!",
        trigger: 'change',
      }
    ],
    verifyRemark: [
      {
        validator: verifyRemarkRule,
        trigger: 'change',
      }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/**
 * 获取货币列表
 */
const moneyList = ref([]);
function getMoneyList() {
  listMoney().then(response => {
    moneyList.value = response.rows;
  });
}
getMoneyList();



/** 查询提现记录列表 */
function getList() {
  loading.value = true;
  listWithdrawRecord(queryParams.value).then(response => {
    withdrawRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

  getWithdrawStatistics(queryParams.value).then(res => {
    statisticsData.value = res.amount;
  })
}

function handleAudit(item) {
  form.value = {
    id: item.id,
    verifyState: 0,
    verifyRemark: '',
  }
  console.log(form.value, 'form')
  open.value = true
}

function submitForm() {
  proxy.$refs["forms"].validate(valid => {
    if (valid) {
      verify({
        ...form.value
      }).then(res => {
        console.log(res, '审核')
        open.value = false;
        getList();
        proxy.$modal.msgSuccess("审核成功");
      })
    }
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('admin/withdrawRecord/export', {
    ...queryParams.value
  }, `withdrawRecord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
